from . import app_rendering
from flask import render_template, redirect, request
from token_mmwl import Token
from conn_mysql import ConnectMysql

@app_rendering.route("/personal_update", methods = ["GET", "POST"])

def personal_update():

    if request.method == 'GET':

        token = request.args.get("token")

        info = Token().token_status(token)

        if info["status"] == -1:

            return redirect(f'/login?msg={info["errorMsg"]}')

        
        return render_template('personal_update.html', 
                                token = token, 
                                info = info['resultBody']['company_info'])

    elif request.method == "POST":
        token = request.form.get('token')
        party = request.form.get("party")
        need = request.form.get("need")
        product_name = request.form.get("product_name")

        info = Token().token_status(token)

        if info["status"] == -1:
    
            return redirect(f'/login?msg={info["errorMsg"]}')

        with ConnectMysql() as CM:

            sql = "UPDATE company SET party=%s, need=%s, product_name=%s WHERE id=%s"

            CM.update_info(sql,(party, need, product_name, info['resultBody']['company_info']['id']))

        return redirect(f'/personal_update?token={token}')